package core;

import java.util.ArrayList;
import java.util.List;

public class Team {

	public static int idGenerator=1;
	private int id;
	private String name;
	private int nbWin;
	private int nbNull;
	private int nbLoose;
	private int nbPoints;
	private boolean abandonned;
	private List<Integer> havePlayedAgainst=new ArrayList<Integer>();
	
	public static int NB_LOOSE_POINT=0;
	public static int NB_NULL_POINT=1;
	public static int NB_WIN_POINT=2;
	
	
	protected Team(){		
	}
	
	public List<Integer> getAdversaire(){
		return havePlayedAgainst;
	}
	
	public Team(String playersName){
		name=playersName;
		id=idGenerator++;
		nbWin=0;
		nbLoose=0;
		nbPoints=0;
		nbNull=0;
		abandonned= false;
	}
	
	public Team(int id, String name, int nbLoose, int nbNull, int nbWin,
			int nbPoint, boolean abandone, List<Integer> adversaire) {
		this.id=id;
		this.name=name;
		this.nbLoose=nbLoose;
		this.nbNull=nbNull;
		this.nbWin=nbWin;
		nbPoints=nbPoint;
		abandonned=abandone;
		havePlayedAgainst=adversaire;
	}

	public int getWinPoints(){
		return NB_WIN_POINT*nbWin+NB_NULL_POINT*nbNull+NB_LOOSE_POINT*nbLoose;
	}
	
	public boolean haveAbandonned(){
		return abandonned;
	}
	
	public void abandonne(){
		this.abandonned=true;
	}

	public int getId(){
		return id;
	}
	
	public String toString(){
		return id + " - " + name;
	}
	
	public void win(int points,int otherTeamId){
		havePlayedAgainst.add(otherTeamId);
		nbWin++;
		nbPoints+=points;
	}
	
	public int getNbWin(){
		return nbWin;
	}
	
	public int getNbPoints(){
		return nbPoints;
	}
	
	public int getNbNull(){
		return nbNull;
	}
	
	public int getNbLoose(){
		return nbLoose;
	}
	
	public void loose(int points,int otherTeamId){
		havePlayedAgainst.add(otherTeamId);
		nbLoose++;
		nbPoints+=points;
	}
	
	public void nullMatch(int points,int otherTeamId){
		havePlayedAgainst.add(otherTeamId);
		nbNull++;
		nbPoints+=points;
	}
	
	public static final int isBetterThan(Team t1,Team t2){
		int i=t2.getWinPoints()-t1.getWinPoints();
		if(i!=0)
			return i;
		i =t2.nbWin - t1.nbWin ;
		if (i!=0)
			return i;
		i = t2.nbNull-t1.nbNull;
		if (i!=0)
			return i;
		return t2.nbPoints-t1.nbPoints;
	}
	
	public boolean havePlayedAgainst(int teamId){
		for(Integer i:havePlayedAgainst)
			if(i==teamId)
				return true;
		return false;
	}
	
	public int nbMatchVs(int teamId){
		int res=0;
		for(Integer i:havePlayedAgainst)
			if(i==teamId)
				res++;
		return res;
	}
	
	public void reprendre(){
		abandonned=false;
	}
	
	public String getName(){
		return name;
	}
	
}
